package com.bwsoft.portal.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.VerificationCode;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.util.Constant;
import com.bwsoft.portal.service.RegisterService;
import com.bwsoft.util.MD5Util;
import com.bwsoft.util.UploadFileUtil;

@Service(RegisterService.NAME)
public class RegisterServiceImpl extends PortalBaseServiceImpl implements
	RegisterService {
	private static Logger LOG = Logger.getLogger(RegisterServiceImpl.class);

	@Override
	public ResponsePojo regCompany(Account account, int accountType, String compName, String linkMan, String phone,
			String address, String email, String password, String fax,
			Integer compNature, String cnasFlag, File img1, String img1FileName, File img2, String img2FileName) {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		ResponsePojo rp = new ResponsePojo();
		try {
			Calendar now = Calendar.getInstance();
			if(img1FileName != null && !"".equals(img1FileName)){
				String img1Realpath = ServletActionContext.getServletContext().getRealPath(Constant.PATH_IMG_SHANGCHUAN)+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1);
				String img1RealName=null ;
				img1RealName=UUID.randomUUID().toString()+img1FileName.substring(img1FileName.lastIndexOf('.'), img1FileName.length());
				UploadFileUtil.uploadOneFile(img1RealName, img1, img1Realpath);
				account.setImg1(Constant.PATH_IMG_SHANGCHUAN+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1)+"/"+img1RealName);
			}
			if(img2FileName != null && !"".equals(img2FileName)){
				String img2Realpath = ServletActionContext.getServletContext().getRealPath(Constant.PATH_IMG_SHANGCHUAN)+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1);
				String img2RealName=null ;
				img2RealName=UUID.randomUUID().toString()+img2FileName.substring(img2FileName.lastIndexOf('.'), img2FileName.length());
				UploadFileUtil.uploadOneFile(img2RealName, img2, img2Realpath);
				account.setImg2(Constant.PATH_IMG_SHANGCHUAN+"/"+now.get(Calendar.YEAR)+(now.get(Calendar.MONTH) + 1)+"/"+img2RealName);
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
			account.setAccountType(accountType);
			account.setAccount(email);
			account.setNickName(email);
			account.setCompName(compName);
			account.setLinkMan(linkMan);
			account.setPhone(phone);
			account.setAddress(address);
			account.setEmail(email);
			account.setPassword(MD5Util.md5_16(password));
			account.setFax(fax);
			account.setCompNature(compNature);
			if ("on".equals(cnasFlag))
				account.setCnasFlag(true);
			else
				account.setCnasFlag(false);
			account.setAttestStatus(1);
			account.setJinyongFlag(false);
			account.setAddTime(sdf.format(new Date()));
			account.setHeadImgURL("file/img/morentouxiang.jpg");
			account.setAttestStatus(0);
			accountDao.saveOrUpdate(account);
			session.setAttribute("Account", account);
			rp.setErrcode(0);
			rp.setErrmsg("保存成功！");
		} catch (Exception e) {
			rp.setErrcode(14);
			rp.setErrmsg("保存失败");
			LOG.info(e);
		}
		return rp;
	}

	@Override
	public ResponsePojo regPerson(Account regAccount, int accountType,
			String mobile, String loginpwd) {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		ResponsePojo rp = new ResponsePojo();
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		try {
			regAccount.setPhone(mobile);
			regAccount.setNickName(mobile);
			regAccount.setAccount(mobile);
			regAccount.setSex(true);
			regAccount.setPassword(MD5Util.md5_16(loginpwd));
			regAccount.setAccountType(accountType);
			regAccount.setJinyongFlag(false);
			regAccount.setBirthday(new Date());
			regAccount.setAddTime(sdf.format(new Date()));
			regAccount.setHeadImgURL("file/img/morentouxiang.jpg");
			accountDao.saveOrUpdate(regAccount);
			session.setAttribute("Account", regAccount);
			rp.setErrcode(0);
			rp.setErrmsg("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rp;
	}

	@Override
	public boolean checkCode(String mobile, String checkcode) {
		String checkHql="from VerificationCode as a where a.number=? and code=? and status=1 and TIMESTAMPDIFF(MINUTE, sendTime, now())<=30";
		
		List<VerificationCode> verificationCodes=verificationCodeDao.findByHql(checkHql, mobile, checkcode);
		
		if(verificationCodes.size()==0){
			return false;
		} else {
			return true;
		}
	}
	
	@Override
	public boolean checkAccount(String account) {
		List<Account> accounts = (List<Account>)accountDao.findByHql("from Account where account=?", account);
		if (accounts.size() > 0)
			return true;
		else
			return false;
	}
	
}
